package com.sczmgk.hotline.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sczmgk.hotline.R;
import com.sczmgk.hotline.entity.ArticleInfo;
import com.sczmgk.hotline.entity.Picurls;

import java.util.List;

import static com.sczmgk.hotline.MyApp.bitmapUtils;

public class ArticleAdapter extends BaseAdapter {

    private Context context;
    private List<ArticleInfo> lists;

    public ArticleAdapter(Context context , List<ArticleInfo> infos){
        this.context = context ;
        this.lists = infos;
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_center);
    }

    public void setList(List<ArticleInfo> infos){
        this.lists = infos;
    }

    @Override
    public int getCount() {
        return lists == null ? 0 : lists.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        ViewHolder holder = null ;

        if (convertView == null){
            convertView = View.inflate(context, R.layout.item_article,null);
            holder = new ViewHolder();
            holder.tvPublishTime = (TextView) convertView.findViewById(R.id.tv_item_article_time);
            holder.tvArticleName = (TextView) convertView.findViewById(R.id.tv_item_article_name);
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_item_article_image);
            holder.tvContent = (TextView) convertView.findViewById(R.id.tv_item_article_content);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_item_article_title);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        ArticleInfo info = lists.get(i);

        holder.tvTitle.setText(info.title);
        holder.tvContent.setText(info.content);
        holder.tvArticleName.setText(info.articlename);
        holder.tvPublishTime.setText(info.publishtime);

        Picurls picurls = info.picurls;
        if (picurls != null) {
            bitmapUtils.display(holder.imageView, picurls.smallpic);
        }else{
            holder.imageView.setImageResource(R.drawable.default_center);
        }

        return convertView;
    }

    private class ViewHolder{
        public TextView tvTitle ;
        public TextView tvContent ;
        public TextView tvArticleName ;
        public TextView tvPublishTime ;
        public ImageView imageView;
    }
}